<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<style>
</style>
<script type="text/javascript">
    var articleDataGrid;
    var articleTypeTree;
    $(function() {
    	articleTypeTree = $('#articleTypeTree').tree({
             url : '${path }/articleType/tree',
             parentField : 'pid',
             lines : true,
             onClick : function(node) {
            	 articleDataGrid.datagrid('load', {
                     type: node.id
                 });
             }
         });
        articleDataGrid = $('#articleDataGrid').datagrid({
            url : '${path}/article/dataGrid',
            striped : true,
            rownumbers : true,
            pagination : true,
            singleSelect : true,
            idField : 'id',
            sortName : 'id',
            sortOrder : 'asc',
            pageSize : 20,
            pageList : [ 10, 20, 30, 40, 50, 100, 200, 300, 400, 500 ],
            columns : [ [
                {
                width : '100',
                title : 'id',
                field : 'id',
                hidden:true
            },
                {
                width : '300',
                title : '标题',
                field : 'title'
            },
                {
                width : '100',
                title : '类型',
                field : 'type'
                ,hidden : true
            },
                {
                width : '100',
                title : '类型',
                field : 'typeName'
            },
                {
                width : '200',
                title : '标签',
                field : 'tags'
            }
            ]],
            frozenColumns : [ [{
                field : 'action',
                title : '操作',
                width : 200,
                formatter : function(value, row, index) {
                    var str = '';
                    str += $.formatString('<a href="javascript:void(0)" class="article-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'fi-pencil icon-blue\'" onclick="editArticleFun(\'{0}\');" >编辑</a>', row.id);
                    str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                    str += $.formatString('<a href="javascript:void(0)" class="article-easyui-linkbutton-del" data-options="plain:true,iconCls:\'fi-x icon-red\'" onclick="deleteArticleFun(\'{0}\');" >删除</a>', row.id);
                      str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                    str += $.formatString('<a href="javascript:void(0)" class="article-easyui-linkbutton-view" data-options="plain:true,iconCls:\'fi-magnifying-glass\'" onclick="viewArticleFun(\'{0}\',\'{1}\');" >查看</a>', row.id,row.title);
                    return str;
                }
            } ] ],
            onLoadSuccess:function(data){
                $('.article-easyui-linkbutton-edit').linkbutton({text:'编辑'});
                $('.article-easyui-linkbutton-del').linkbutton({text:'删除'});
                $('.article-easyui-linkbutton-view').linkbutton({text:'查看'});
            },
            toolbar : '#articleToolbar'
        });
    });

    function addArticleFun() {
      var url="${path}/article/addPage";
      var opts = {
              title : '发布文章',
              border : false,
              closable : true,
              fit : true,
       };
      opts.content = '<iframe src="' + url + '" frameborder="0" style="border:0;width:100%;height:99%;overflow:hidden" id="articleAddIframe" ></iframe>';
      addTab(opts);
      iframeLoad("articleAddIframe");
    }

    function editArticleFun(id) {
        if (id == undefined) {
            var rows = articleDataGrid.datagrid('getSelections');
            id = rows[0].id;
        } else {
            articleDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
        }
        var url="${path}/article/editPage/"+id;
        var opts = {
                title : '编辑发布文章',
                border : false,
                closable : true,
                fit : true,
         };
        opts.content = '<iframe src="' + url + '" frameborder="0" style="border:0;width:100%;height:99%;overflow:hidden" id="articleEditIframe" ></iframe>';
        addTab(opts);
        iframeLoad("articleEditIframe");
    }

    function deleteArticleFun(id) {
        if (id == undefined) {//点击右键菜单才会触发这个
            var rows = articleDataGrid.datagrid('getSelections');
            id = rows[0].id;
        } else {//点击操作里面的删除图标会触发这个
            articleDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
        }
        parent.$.messager.confirm('询问', '您是否要删除吗？', function(b) {
            if (b) {
                progressLoad();
                $.post('${path}/article/delete/'+id, {}
                , function(result) {
                    if (result.success) {
                        parent.$.messager.alert('提示', result.msg, 'info');
                        articleDataGrid.datagrid('reload');
                    }
                    progressClose();
                }, 'JSON');
            }
        });
    }
     function viewArticleFun(id,name) {
        if (id == undefined) {
            var rows =  articleDataGrid.datagrid('getSelections');
            id = rows[0].id;
        } else {
             articleDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
        }
        <%--parent.$.modalDialog({--%>
            <%--title : '查看',--%>
            <%--width : '80%',--%>
            <%--height : '80%',--%>
            <%--href : '${path}/article/viewPage/' + id,--%>
        <%--});--%>
         var opts = {
             title : name,
             border : false,
             closable : true,
             fit : true,
             iconCls:'fi-page',
             content:'<iframe   src="${path}/article/viewPage/'+id+'"  style="border:0;width:100%;height:99%;overflow:hidden" id="doc_read_file_'+id+'"></iframe>'
         };
         parent.addTab(opts);
    }

    function exportArticleFun(){
      var row= articleDataGrid.datagrid('getSelected');
      if(!isNull(row)){
          var id=row.id;
          progressLoad();
         $.ajax({
            url:'${ctxPath}/article/export2PDF2?id='+id,
            dataType:'json',
            success:function(data){
                progressClose();
                if(data.success){
                   var name=data.obj[0];
                   var path=data.obj[1];
                   var form=document.createElement('form');
                   $(form).attr('action','${ctxPath}/article/download');
                   $(form).attr('method','post');
                   var input1=document.createElement('input');
                   $(input1).attr('name','name');
                    $(input1).attr('value',name);
                    form.appendChild(input1);
                    var input2=document.createElement('input');
                    $(input2).attr('name','path');
                    $(input2).attr('value',path);
                    form.appendChild(input2);
                    document.body.appendChild(form);
                    form.submit();
                }else{
                    $.messager.alert('提示',data.msg,'error');
                }
            }
        })
      }else{
          $.messager.alert('提示','请选择一条记录','info');
      }
    }

    /**
     * 导出word
     * @param id
     */
    function exportArticleWordFun(){
        var row= articleDataGrid.datagrid('getSelected');
        if(!isNull(row)){
            var id=row.id;
            window.location.href='${ctxPath}/article/export2Word?id='+id;
        }else{
            $.messager.alert('提示','请选择一条记录','info');
        }
    }

    /**
     * 导出markdown
     * @param id
     */
    function exportArticleMDFun(){
        var row= articleDataGrid.datagrid('getSelected');
        if(!isNull(row)){
            var id=row.id;
            window.location.href='${ctxPath}/article/exportMd?id='+id;
        }else{
            $.messager.alert('提示','请选择一条记录','info');
        }
    }

    function searchArticleFun() {
        articleDataGrid.datagrid('load', $.serializeObject($('#searchArticleForm')));
    }

    function cleanArticleFun() {
        $('#searchArticleForm input').val('');
        articleDataGrid.datagrid('load', {});
    }


</script>
<div class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'west',border:true,split:true,title:'文档类型'"  style="width:200px;overflow: hidden; overflow-y:auto; padding:0px">
        <ul id="articleTypeTree" style="width:160px;margin: 10px 10px 10px 10px"></ul>
    </div>
      <div data-options="region:'center',border:true,title:'文档列表'">
        <table id="articleDataGrid" data-options="fit:true,border:false"></table>
    </div>
</div>
<div id="articleToolbar" style="display: none;">
    <form id="searchArticleForm">
        <table>
            <tr>
                <td>文件名称:</td>
                <td><input name="name" placeholder="" class="easyui-text" style="width:200px"/>
                    <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fi-magnifying-glass',plain:true" onclick="searchArticleFun();">查询</a>
                    <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fi-x-circle',plain:true" onclick="cleanArticleFun();">清空</a>
                </td>
            </tr>
        </table>
    </form>
    <a onclick="addArticleFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-plus icon-green'">添加</a>
    <a onclick="exportArticleFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-page-export-pdf icon-green'">导出PDF</a>
    <a onclick="exportArticleWordFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-page-export-doc icon-green'">导出WORD</a>
    <a onclick="exportArticleMDFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-page-export-doc icon-green'">导出MD</a>
</div>
